//
//  AudioPlay.cpp
//  client
//
//  Created by DuanHongbo on 15/9/10.
//
//

#include "AudioPlay.h"
#include "AudioEngine.h"

AudioPlay::AudioPlay():_audioId(-1)
{
    
}

AudioPlay::~AudioPlay()
{
    if(_audioId>=0)
    {
        cocos2d::experimental::AudioEngine::stop(_audioId);
        _audioId = -1;
    }
}

AudioPlay* AudioPlay::create(float d, const std::string &file)
{
    AudioPlay *p_action = new AudioPlay();
    p_action->initWithDuration(d,file);
    p_action->autorelease();
    return p_action;
}

bool AudioPlay::initWithDuration(float d, const std::string& file)
{
    if (ActionInterval::initWithDuration(d))
    {
        _audioFile = file;
        return true;
    }
    return false;
}

void AudioPlay::startWithTarget(Node *pTarget)
{
    ActionInterval::startWithTarget( pTarget );
    if(_audioFile.size()>0 && !UserDefault::getInstance()->getBoolForKey("AM.isMuteSound",false))
    {
        float volume = UserDefault::getInstance()->getFloatForKey("AM.SoundVolumeMax",1.0f);
        _audioId = cocos2d::experimental::AudioEngine::play2d(_audioFile,true);
        cocos2d::experimental::AudioEngine::setVolume(_audioId, volume);
    }
}

void AudioPlay::update(float time)
{

}


void AudioPlay::stop(void)
{
    if(_audioId>=0)
    {
        cocos2d::experimental::AudioEngine::stop(_audioId);
        _audioId = -1;
    }
    ActionInterval::stop();
}
